//
//  SettingListVc.m
//  WRY
//
//  Created by zhongaibing on 3/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SettingListVc.h"
#import "RootVC_iPad.h"
#import "AppDelegate_iPad.h"
#import "CommonJson.h"
#import "WebPath.h"
#import "SettingPathVC_iPad.h"

@interface SettingListVc ()

@end

@implementation SettingListVc
@synthesize arraySettings,rootVc;


- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    NSArray *array=[[NSArray alloc]initWithObjects:@"设备解绑",@"服务器地址",nil];
    self.arraySettings=array;
    [array release];
    
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" 
															  style:UIBarButtonItemStyleBordered 
															 target:self 
															 action:@selector(backPage:)];
	self.navigationItem.leftBarButtonItem=leftItem;
    [leftItem release];
    
    
}
-(void)backPage:(id)sender
{
	
    
    [rootVc.popController dismissPopoverAnimated:YES];
    
	
}
-(void)dealloc
{
    [super dealloc];
    [arraySettings release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arraySettings count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"settingsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell==nil) {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    }
    if (self.arraySettings !=nil && [arraySettings count]>0) {
        cell.textLabel.text=[arraySettings objectAtIndex:indexPath.row];
    }
    return cell;
}

-(void)jiebangDevice
{
    [self showLoading:0 centerY:0];
	NSString *body = [NSString stringWithFormat:@"LoginGuid=%@",[[AppDelegate_iPad currentDelegate]GUID]];
	[[CommonJson sharedInstance] requestToPath:[WebPath loginPath]
									methodName:@"UnbindDevice"
									bodyString:body 
									 targetObj:self 
							  targetMethodName:@"eventForUnbindDevice:" 
									notifiName:@"UnbindDevice"];
}
-(void)eventForUnbindDevice:(NSNotification *)notification
{
    [self hideLoading];
    [[CommonJson sharedInstance]removeTarget:self notiName:[notification name]];
  
	NSObject *object = [notification object];
	if ([object isKindOfClass:[NSDictionary class]]) {
		NSDictionary *dict = (NSDictionary *)object;
		if ([[dict objectForKey:@"Status"] intValue]==1) {
			NSString *result=[dict objectForKey:@"Data"];
            if ([result intValue]==1) {
                //解绑成功
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示信息" message:@"解绑成功，重新登录!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                alert.delegate=self;
                alert.tag=1;
                [alert show];
                [alert release];
            }
		}
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if (buttonIndex==0) {
            [[AppDelegate_iPad currentDelegate]setGUID:@""];
            [[AppDelegate_iPad currentDelegate]restartApplication];
        }
       
    }
   
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0) {
        //解绑设备
        [self jiebangDevice];
    }
    else if(indexPath.row==1)
    {
        SettingPathVC_iPad *pathVc=[[[SettingPathVC_iPad alloc]initWithNibName:@"SettingPathVC_iPad" bundle:nil]autorelease];
        pathVc.fromTag=200;
        [pathVc setContentSizeForViewInPopover:CGSizeMake(280,300)];
        [self.navigationController pushViewController:pathVc animated:YES];
    }
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end
